package pay

import (
	"chatgpt/helper/code"
	"chatgpt/logq"
	"chatgpt/model/app"
	dbapp "chatgpt/services/datasrv/app"
	"chatgpt/services/datasrv/sqlhelper"
	"context"
)

// 充值记录表
type RechargeSrv struct {
}

func (self *RechargeSrv) GetListDB(where *sqlhelper.Where) []*app.Recharge {
	datapp := dbapp.AppApi{}
	res := sqlhelper.ResWhere{}
	mc := datapp.ProductSelect(context.Background(), &sqlhelper.ReqWhere{
		Where: where,
	}, &res)
	if res.MsgCode != code.SUCCESS {
		logq.InfoF("Recharge.GET code:%v", mc)
		return nil
	}
	data_arr, ok := res.Data.([]any)
	if !ok {
		return nil
	}

	rtn := []*app.Recharge{}
	for _, v := range data_arr {
		m, ok := v.(map[string]any)
		if !ok {
			continue
		}
		tmp := &app.Recharge{}
		tmp.ConvFromMap(m)
		rtn = append(rtn, tmp)
	}
	return rtn
}

func (self *RechargeSrv) Save(p *app.Recharge) code.CODENO {
	datapp := dbapp.AppApi{}
	res := sqlhelper.ResWhere{}
	mc := datapp.ProductSave(context.Background(), &sqlhelper.ReqWhere{
		Object: p.ConvToMap(),
		Where: &sqlhelper.Where{
			Conds: []*sqlhelper.CondItem{sqlhelper.WhereCond("ID", p.ID)},
		},
	}, &res)
	if res.MsgCode != code.SUCCESS {
		logq.InfoF("Recharge.SAVE code:%v", mc)
		return res.MsgCode
	}
	return code.SUCCESS
}

func (self *RechargeSrv) FirstDB(where *sqlhelper.Where) *app.Recharge {
	datapp := dbapp.AppApi{}
	res := sqlhelper.ResWhere{}
	mc := datapp.ProductFirst(context.Background(), &sqlhelper.ReqWhere{
		Where: where,
	}, &res)
	if res.MsgCode != code.SUCCESS {
		logq.InfoF("Recharge.First code:%v", mc)
		return nil
	}
	data_arr, ok := res.Data.(map[string]any)
	if !ok {
		return nil
	}
	rtn := &app.Recharge{}
	rtn.ConvFromMap(data_arr)
	return rtn
}
